/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package listenerthread;

import java.io.PrintWriter;


import static klassetvserver.ServerListener.clientArray;
import static klassetvserver.ServerListener.clientLobbyArray;

/**
 *
 * @author Nick
 */
public class ServerListenerThreadOut implements Runnable {

    private final PrintWriter out;
    private final ServerListenerThread main;

    public ServerListenerThreadOut(PrintWriter out, ServerListenerThread main) {
        this.out = out;
        this.main = main;


    }

    public void run() {
        //Send the clientId
        out.println(main.getClientId());
    }

    public void requestGame(ServerListenerThread opponent) {
        synchronized (out) {
            out.println("requestGame");
            main.setRequestOpponent(opponent);
            out.println(opponent.getClientName());
            System.out.println("SENT CLIENT NAME");
            out.println(opponent.getClientId());
            System.out.println("SENT CLIENT ID");
        }

    }

    public void statusChange(int status) {
        synchronized (out) {
            out.println("currentStatusChange");
            out.println(status);
        }
    }

    public void acceptGame() {
        main.setOpponent(clientArray[main.getRequestedOpponentId()]);
        out.println("acceptGame");
    }

    public void rejectGame() {
        out.println("rejectGame");
    }

    public void setOpponentCurrentImageAndGuessedLetters(int imageCount, String guessedLetters) {
        synchronized (out) {
            out.println("setOpponentCurrentImageAndGuessedLetters");
            out.println(imageCount);
            out.println(guessedLetters);
        }
    }

    public void sendMessage(String message) {
        synchronized (out) {
            out.println("chatMessage");
            out.println(message);
        }
    }

    public void setWord(String word) {
        synchronized (out) {
            out.println("setWord");
            out.println(word);
        }
    }

    public void updateLobby() {
        if (out != null) {
            out.println("notifyLobbyUpdate");
        }

    }

    public void getLobby() {
        synchronized (out) {
            out.println("getLobby");
            for (int i = 0; i < clientLobbyArray.length; i++) {

                if (clientLobbyArray[i] != null) {

                    int p = clientLobbyArray[i].getClientId();
                    System.out.print("LIST ITEM: ID: " + p);
                    out.println(p);
                    String name = clientLobbyArray[i].getClientName();
                    System.out.println(", NAME: " + name);
                    out.println(name);
                }
            }

            out.println("END");
        }
    }
}
